package org.lq.ht.view.viewSale;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.lq.ht.entity.Sales;
import org.lq.ht.entity.Vip;
import org.lq.ht.service.CustomerService;
import org.lq.ht.service.DictionaryService;
import org.lq.ht.service.SalesService;
import org.lq.ht.service.VipService;
import org.lq.ht.service.impl.CustomerServiceImpl;
import org.lq.ht.service.impl.DictionaryServiceImpl;
import org.lq.ht.service.impl.SalesServiceImpl;
import org.lq.ht.service.impl.VipServiceImpl;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SelectSales extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JPopupMenu popupMenu;
	private JMenuItem mntmNewMenuItem;

	private int roomId;
	public int getRoomID() {
		return roomId;
	}
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SelectSales frame = new SelectSales();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SelectSales() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		table = new JTable();
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null},
			},
			new String[] {
				"\u623F\u95F4\u53F7", "\u987E\u5BA2\u7F16\u53F7", "\u540D\u5B57", "vip\u7F16\u53F7"
			}
		));
		scrollPane.setViewportView(table);
		
		popupMenu = new JPopupMenu();
		addPopup(table, popupMenu);
		
		mntmNewMenuItem = new JMenuItem("选择");
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//选择
				int index = table.getSelectedRow();
				roomId = (int) table.getValueAt(index, 0);
				dispose();
			}
		});
		popupMenu.add(mntmNewMenuItem);
		
		initTable();
	}
	
	public  void initTable() {

        SalesService ss = new SalesServiceImpl();
        List<Sales> list =ss.findRoom();
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();

		int count = dtm.getRowCount();
		for (int i = 0; i < count; i++) {
			dtm.removeRow(0);
		}
        CustomerService cs = new CustomerServiceImpl();
		for (Sales s : list) {
			dtm.addRow(new  Object[] {
					s.getRoom_id(),
					s.getCustomer_id(),
					cs.getById(s.getCustomer_id()).getName(),
					s.getVip_card()
			});
		}

	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
